package com.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



import com.ysh.gsm.vos.ThreeSerice;

public class TrDataCalc {
	
	public static List<ThreeSerice> calTotal(List<TrMrData> ta){
		
		Collections.sort(ta, new CustomComparator());
		List<ThreeSerice>result=new ArrayList<ThreeSerice>();
		double vy1=0;
		double vy2=0;
		double sumUp=0;
		double sumDn=0;
		int count=0;
		long sumcount=0l;
		int len=ta.size();
		for(int i=0;i<len;i++){
			count=ta.get(i).getCount();
			sumcount+=count;
			sumUp+=ta.get(i).getRxup()*count;
			sumDn+=ta.get(i).getRxdn()*count;
			if(i==len-1){
				vy1=(double)sumUp/sumcount;
				vy2=(double)sumDn/sumcount;
				result.add(new ThreeSerice(ta.get(i).getVal(),vy1,vy2,count));
				break;
			}
			if(ta.get(i).getVal()<ta.get(i+1).getVal()){
				vy1=(double)sumUp/sumcount;
				vy2=(double)sumDn/sumcount;
				result.add(new ThreeSerice(ta.get(i).getVal(),vy1,vy2,count));
				sumUp=0;
				sumDn=0;
				sumcount=0;
			}
		
		}
		
		return result;
		
	}
	private static class CustomComparator implements Comparator<DbMRData> {

		public int compare(DbMRData o1, DbMRData o2) {
			return (o1.getVal() > o2.getVal() ? 1 : (o1.getVal() == o2
					.getVal() ? 0 : -1));

		}
	}

}
